package wiki.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.utilities.PageNotFoundException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile dell'eliminazione di contenuti dalle pagine
 */
public class DeletingServlet extends HttpServlet{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc=(Boolean) ses.getAttribute("rights");
				if(isDoc){
					String current=(String) ses.getAttribute("current");
					String corso=(String) ses.getAttribute("corso");
					String name=req.getParameter("name");
					String type=req.getParameter("type");
					if(type!=null){
						if(type.compareTo("category")==0){
							Setup.getSetup().getMediator().getbService().deleteSideBarCategory(corso,name);
						}
						if(type.compareTo("link")==0){
							String cat=req.getParameter("cat");
							Setup.getSetup().getMediator().getbService().deleteLink(corso,cat, name);
						}
						resp.sendRedirect("./view?page="+current);
						return;
					} else{
					try {
						Setup.getSetup().getMediator().geteService().deleteContent(corso,current, name);
					} catch (PageNotFoundException e) {
						resp.sendRedirect("./html/errorpages/pagina404.html");
						return;
					}
					}
					resp.sendRedirect("./view?page="+current+"&mod=mod");
	
				} else {
					resp.sendRedirect("./html/errorpages/nopermessi.html");
					return;
				}
		} else {
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
		
	}
		
}
